package com.znsb.msfq.album;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.utils.IntentUtils;
import com.znsb.msfq.utils.ToastUtils;
import com.znsb.msfq.view.selectorphoto.AlbumHelper;
import com.znsb.msfq.view.selectorphoto.Bimp;
import com.znsb.msfq.view.selectorphoto.ImageBucket;
import com.znsb.msfq.view.selectorphoto.ImageItem;
import com.znsb.msfq.view.selectorphoto.PublicWay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 这个是进入相册显示所有图片的界面
 *
 * @author king
 * @version 2014年10月18日 下午11:47:15
 * @QQ:595163260
 */
public class AlbumActivity extends BaseActivity {
    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.preview)
    TextView preview;
    @Bind(R.id.ok_buttonnum)
    TextView okButtonnum;
    @Bind(R.id.ok_buttonlayout)
    LinearLayout okButtonlayout;
    @Bind(R.id.bottom_layout)
    LinearLayout bottomLayout;
    @Bind(R.id.myGrid)
    GridView myGrid;
    @Bind(R.id.myText)
    TextView myText;
    // 显示手机里的所有图片的列表控件
    // 当手机里没有图片时，提示用户没有图片的控件
    // gridView的adapter
    private AlbumGridViewAdapter gridImageAdapter;
    private ArrayList<ImageItem> dataList;
    private AlbumHelper helper;
    public static List<ImageBucket> contentList;
    public static Bitmap bitmap;

    @Override
    protected int getlayoutView() {
        return R.layout.plugin_camera_album;
    }

    @Override
    protected void initView(View view) {
        // 注册一个广播，这个广播主要是用于在GalleryActivity进行预览时，防止当所有图片都删除完后，再回到该页面时被取消选中的图片仍处于选中状态
        IntentFilter filter = new IntentFilter("data.broadcast.action");
        registerReceiver(broadcastReceiver, filter);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.plugin_camera_no_pictures);
        PublicWay.activityList.add(this);
        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());
        myGrid.setEmptyView(myText);
        okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
//        barImgBack.setText("相册");
        barTvOther.setVisibility(View.VISIBLE);
        barTvOther.setText("相册");
        loadData();
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void clickListener(View view) {

    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            gridImageAdapter.notifyDataSetChanged();
        }
    };

    private boolean removeOneData(ImageItem imageItem) {
        if (Bimp.tempSelectBitmap.contains(imageItem)) {
            Bimp.tempSelectBitmap.remove(imageItem);
            okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
            return true;
        }
        return false;
    }

    @Override
    protected void onResume() {
        okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(broadcastReceiver);
        super.onDestroy();
    }

    private void loadData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                contentList = helper.getImagesBucketList(false);
                dataList = new ArrayList<ImageItem>();
                for (int i = 0; i < contentList.size(); i++) {
                    dataList.addAll(contentList.get(i).imageList);
                }
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        gridImageAdapter = new AlbumGridViewAdapter(AlbumActivity.this, dataList,
                                Bimp.tempSelectBitmap);
                        myGrid.setAdapter(gridImageAdapter);
                        gridImageAdapter.setOnItemClickListener(new AlbumGridViewAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(final ToggleButton toggleButton, int position, boolean isChecked,
                                                    Button chooseBt) {
                                if (Bimp.tempSelectBitmap.size() >= PublicWay.num) {
                                    toggleButton.setChecked(false);
                                    chooseBt.setVisibility(View.GONE);
                                    if (!removeOneData(dataList.get(position))) {
                                        ToastUtils.TextToast(AlbumActivity.this, "超出可选图片张数", 2000);
                                    }
                                    return;
                                }
                                if (isChecked) {
                                    chooseBt.setVisibility(View.VISIBLE);
                                    Bimp.tempSelectBitmap.add(dataList.get(position));
                                    okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
                                } else {
                                    Bimp.tempSelectBitmap.remove(dataList.get(position));
                                    chooseBt.setVisibility(View.GONE);
                                    okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
                                }
                                okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
                            }
                        });
                    }
                });
            }
        }).start();
    }

    @OnClick({R.id.bar_img_back, R.id.bar_tv_other, R.id.preview, R.id.ok_buttonlayout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_img_back:
                Bimp.tempSelectBitmap.clear();
                finish();
                break;
            case R.id.bar_tv_other:
                IntentUtils.startActivityAnimGeneral(this, ImageFileActivity.class, null);
                break;
            case R.id.preview:
                if (Bimp.tempSelectBitmap.size() > 0) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("position", "1");
                    IntentUtils.startActivityAnimGeneral(this, ViewGalleryActivity.class, map);
                } else {
                    ToastUtils.TextToast(this, "您未选择图片！", 2000);
                }
                break;
            case R.id.ok_buttonlayout:
                finish();
                break;
        }
    }
}
